package com.yz.xyt.controller;

import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.Constants;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.Clazz;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.vo.ClazzVo;
import com.yz.xyt.service.IBaseDataImportService;
import com.yz.xyt.service.IClazzService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 班级管理
 * @author JW.WANG
 */
@RestController
@RequestMapping("/system")
public class ClazzManagerController {

    @Autowired
    IBaseDataImportService baseDataImportService;

    @Autowired
    IClazzService clazzService;

    @RequestMapping("/importClazz")
    public ResponseResult importClazz(MultipartFile file){
        ResponseResult result = baseDataImportService.importClazz(file);
        return result;
    }

    /**
     * 分页查询班级信息
     * @param classVo
     * @return
     */
    @RequestMapping("/findClazzPage")
    public ResponseResult getClazzPage(ClazzVo classVo){
        ResponseResult result = new ResponseResult();
        PageInfo clazzPage = clazzService.findClazzForPage(classVo);
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("分页查询班级成功");
        result.setData(clazzPage);
        return result;
    }

    /**
     * 查询班级列表
     * @param classVo
     * @return
     */
    @RequestMapping("/findClazzList")
    public ResponseResult getClazzList(ClazzVo classVo){
        ResponseResult result = new ResponseResult();
        try {
            List<ClazzVo> classList = clazzService.findClassList(classVo);
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("查询列表成功");
            result.setData(classList);
        }catch (Exception e){
            result.setCode(HttpStatus.ERROR);
            result.setMsg("查询列表失败");
        }
        return result;
    }

    /**
     * 更新班级
     * @param clazz
     * @return
     */
    @RequestMapping("/updateClazz")
    public ResponseResult updateClazz(Clazz clazz){
        ResponseResult result =  new ResponseResult();
        clazz.setUpdateBy(SecurityUtils.getUsername());
        clazz.setUpdateDate(DateUtils.getNowDate());
       try {
           clazzService.updateByPrimaryKeySelective(clazz);
           result.setCode(HttpStatus.SUCCESS);
           result.setMsg("更新班级成功");
       }catch (Exception e){
           e.printStackTrace();
           result.setCode(HttpStatus.ERROR);
           result.setMsg("更新班级失败");
       }
        return result;
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @RequestMapping("/addClazz")
    public ResponseResult addClazz(Clazz clazz){
        ResponseResult result = new ResponseResult();
        try {
            result = clazzService.addClazz(clazz);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("新增班级失败");
        }
        return result;
    }

    /**
     * 删除班级
     * @param clazzVo
     * @return
     */
    @RequestMapping("/deleteClazz")
    public ResponseResult deleteClazz(ClazzVo clazzVo){
        ResponseResult result = new ResponseResult();
        try {
            result = clazzService.deleteClazz(clazzVo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("删除班级失败");
        }
        return result;
    }

    /**
     * 查询班级信息
     * @param clazzVo
     * @return
     */
    @RequestMapping("/findClazzInfo")
    public ResponseResult findClazzInfo(ClazzVo clazzVo){
        ResponseResult result = new ResponseResult();
        try {
            List<ClazzVo> classList = clazzService.findClassList(clazzVo);
            result.setMsg("查询班级信息成功");
            result.setCode(HttpStatus.SUCCESS);
            result.setData(classList.get(0));
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("查询班级信息失败");
        }
        return result;
    }

    /**
     * 查询年级列表
     * @return
     */
    @RequestMapping("/findGradeList")
    public ResponseResult findGradeList(){
        ResponseResult result = new ResponseResult();
        try {
            List<String> gradeList = clazzService.findGradeList();
            result.setMsg("查询年级成功");
            result.setCode(HttpStatus.SUCCESS);
            result.setData(gradeList);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("查询年级成功失败");
        }
        return result;
    }

}
